/*
 * Copyright 2014 OGIS-RI All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.co.ogis_ri.optaplanner.vehiclerouting;

import jp.co.ogis_ri.optaplanner.core.api.solver.MultiSolver;
import jp.co.ogis_ri.optaplanner.vehiclerouting.domain.VehicleRoutingSolution;

import org.optaplanner.core.api.domain.solution.Solution;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppMulti {

	protected final transient Logger logger = LoggerFactory.getLogger(this
			.getClass());

	AppMultiTerminateEarly appMultiTerminateEarly;

	public static void main(final String[] args) throws NumberFormatException,
			InterruptedException {

		AppMulti app = new AppMulti();

		if (args.length == 0) {
			app.excute(3, 3);
		} else if (args.length >= 2) {
			app.excute(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
		}
	}

	public void excute(final int calculationCount, final int threadCount)
			throws InterruptedException {

		// Build the Solver
		SolverFactory solverFactory = SolverFactory
				.createFromXmlResource("jp/co/ogis_ri/optaplanner/vehiclerouting/solver/config.xml");
		Solver solver = solverFactory.buildSolver();

		// 解く問題を取得
		Solution unsolvedJobSolution = new SolutionGenerator().createSolution();

		// 複数スレッドで同じ問題を複数回解いてみる。
		// 乱数を利用するので毎回結果がことなり、良し悪しの差も出るので
		MultiSolver multiSolver = new MultiSolver(solverFactory,
				unsolvedJobSolution, calculationCount, threadCount);

		// 計算を中断させるボタンを表示
		this.showTerminateEarlyFrame(multiSolver);

		// 複数スレッドで問題を解く
		multiSolver.solve();

		// 最も良い答えを取得
		Solution bestJobSolution = multiSolver.getBestSolution();

		if (bestJobSolution == null) {
			this.logger.error("Solve error.No result.");
			return;
		}

		// 結果を表示
		String result = App
				.toStringSolvedSolution((VehicleRoutingSolution) bestJobSolution);
		String cargoData = App
				.toStringCargoData((VehicleRoutingSolution) bestJobSolution);
		String vehicleData = App
				.toStringVehicleData((VehicleRoutingSolution) bestJobSolution);

		System.out.println(cargoData);
		System.out.println(vehicleData);
		System.out.println(result);

		// 計算を中断させるボタンを消す
		this.shutdownTerminateEarlyFrame();
	}

	/**
	 * 
	 * @param multiSolver
	 */
	private void showTerminateEarlyFrame(final MultiSolver multiSolver) {
		this.appMultiTerminateEarly = new AppMultiTerminateEarly(multiSolver);

		Thread thread = new Thread(this.appMultiTerminateEarly);

		thread.start();
	}

	/**
	 * 
	 */
	private void shutdownTerminateEarlyFrame() {

		this.appMultiTerminateEarly.shutdown();
	}

}
